
const { addZero } = require('../../../utils/util');  // 格式化时间
const CONFIG = require('../../../config');
const util = require('../../../utils/util.js');  
let app = getApp();
var timer = 0;
var topArr=[];
var nowLivePlay = 0;
Component({
	data: {
		Index: 0,
		tableData: [
		
		],
		hotTitle:'热门推荐',
		musicList:{},
		once:false,
		imgUrl : CONFIG.imgUrlAvatar
	},
	properties:{
    obj:{
        type:Object,
        value:'',
        
		},
		obj1:{
			type:Number,
			value:0
		},
		obj2:{
			type:Boolean,
			value:true
		},
		obj3:{
			type:String,
			value:true
		},
  },
  observers : {
    'obj': function (goodsPrices) {
			let that = this
      this.setData({
        musicList:goodsPrices
			});
			// if(this.data.once == false){
			goodsPrices.list.forEach(element => {
				element.url = element.playurl;
				element.videoSrc = element.playurl;
				element.cover  ? element.poster = element.cover[0] : element.poster = '';
				element.startPlay = true;
				// element.play = element.play !=undefined?element.play:false;
				element.sliderValue = 0;
				element.process_duration = "00:00";
				element.total_duration=this.formatSeconds(Number(element.duration)) // 总时长
				element.full = false;
				element.duration=element.total_duration // 总时长
				element.video = false
			});
			that.setData({
				tableData:goodsPrices.list
			})
			for(let i=0;i<this.data.tableData.length;i++){
				this.setData({
					['tableData['+i+'].comment']:util.toFix(Number(this.data.tableData[i].comment),10000,'w')
				})
				this.setData({
					['tableData['+i+'].star']:util.toFix(Number(this.data.tableData[i].star),10000,'w')
				})
			// }
			}
			if(goodsPrices.list.length>0){
				nowLivePlay = goodsPrices.list[0].id
			}
			topArr = topArr.sort((x, y) => {
        return x - y
      })
      if (timer != 0) {
        clearTimeout(timer);
      }
      timer = setTimeout(function() {
				let Index_num = that.data.Index
        for (let j = 0; j < topArr.length; j++) {
          if (topArr[j] > goodsPrices.scrollTop && goodsPrices.scrollTop>(wx.getSystemInfoSync().windowHeight/2)) {
            that.setData({
              Index: that.data.tableData[j].id
            })
						let string0 = 'myVideo' + nowLivePlay + ''
						let videoContext0 = wx.createVideoContext(string0,that);
            videoContext0.stop();
            let string1 = 'myVideo' + that.data.tableData[j].id + ''
            let videoContext1 = wx.createVideoContext(string1,that);
						videoContext1.play();
						that.triggerEvent('stop', true);
						nowLivePlay = that.data.tableData[j].id
						that.setData({
							['tableData['+j+'].play'] : true
						})
            return;
          }
        }
      }, 500);
		},
		'obj1':function(goodsPrices){
			let that = this
			
			topArr = topArr.sort((x, y) => {
        return x - y
      })
      if (timer != 0) {
        clearTimeout(timer);
      }
      timer = setTimeout(function() {
				let Index_num = that.data.Index
        for (let j = 0; j < topArr.length; j++) {
          if (topArr[j] > goodsPrices && goodsPrices>(wx.getSystemInfoSync().windowHeight/2)) {
            that.setData({
              Index: that.data.tableData[j].id
            })
						let string0 = 'myVideo' + nowLivePlay + ''
						let videoContext0 = wx.createVideoContext(string0,that);
            videoContext0.stop();
            let string1 = 'myVideo' + that.data.tableData[j].id + ''
            let videoContext1 = wx.createVideoContext(string1,that);
						videoContext1.play();
						that.triggerEvent('stop', true);
						nowLivePlay = that.data.tableData[j].id
						that.setData({
							['tableData['+j+'].play'] : true
						})
            return;
          }
        }
      }, 500);
		},
		'obj2':function(goodsPrices){
			if(goodsPrices == false){
				let that = this
				nowLivePlay = this.data.tableData[0].id
				topArr = topArr.sort((x, y) => {
					return x - y
				})
				if (timer != 0) {
					clearTimeout(timer);
				}
				timer = setTimeout(function() {
					let Index_num = that.data.Index
					for (let j = 0; j < topArr.length; j++) {
						let string1 = 'myVideo' + that.data.tableData[j].id + ''
            let videoContext1 = wx.createVideoContext(string1,that);
						videoContext1.stop();
					}
				}, 500);
			}
		},
		'obj3':function(goodsPrices){
			console.log(goodsPrices)
			if(goodsPrices != ''){
				this.setData({
					hotTitle:goodsPrices
				});
			}
		}
	},
	// 组件生命周期
  lifetimes: {
    // 在组件实例进入页面节点树时执行
    created() {
      
    },
    attached() {
			setTimeout(()=>{
				this.getId()
			},500)
    },
    
  },
	onReady: function() {
		
	},
	methods:{
		todetail: function(e){
      const { id } = e.currentTarget.dataset;
      wx.navigateTo({
        url: `/pages/shortVideo/index?number=${id}`
      })
    },
		getId: function() {
			let top_num=this.data.tableData.length
			console.log(top_num)
			for (let i =0; i < top_num; i++) {
				topArr[i] = 0;
				const query = wx.createSelectorQuery().in(this)
				query.select('#myVideo'+this.data.tableData[i].id).boundingClientRect() //获取节点位置信息的查询请求
				query.selectViewport().scrollOffset(); // 获取页面滑动位置的查询请求
				query.exec((res) => {
					topArr[i] = res[0].top; //节点的上边界坐标
				})
			}
		},
		onShareAppMessage: function (from) {
			if(from == 'button'){
				const promise = new Promise(resolve => {
					setTimeout(() => {
						resolve({
							title: '自定义转发标题'
						})
					}, 2000)
				})
				return {
					title: '自定义转发标题',
					path: '/page/user?id=123',
					promise 
				}
			}else{
				const promise = new Promise(resolve => {
					setTimeout(() => {
						resolve({
							title: '自定义转发标题'
						})
					}, 2000)
				})
				return {
					title: '自定义转发标题',
					path: '/page/user?id=123',
					promise 
				}
			}
		},
		// 播放
		// playStart(e) {
		// 	for(var i = 0; i<this.data.tableData.length; i++){
		// 		if (e.currentTarget.dataset['index'] === this.data.tableData[i].id){
		// 			if(this.data.tableData[i].play == false){
		// 				this.setData({
		// 					['tableData['+i+'].play'] : true
		// 				})
		// 				let videoId = 'myVideo'+this.data.tableData[i].id;
		// 				let videoContext1 = wx.createVideoContext(videoId,this);
		// 				videoContext1.play();
		// 				this.triggerEvent('stop', true);
		// 			}else{
		// 				this.setData({
		// 					['tableData['+i+'].play'] : false
		// 				})
		// 				let videoId = 'myVideo'+this.data.tableData[i].id;
		// 				let videoContext1 = wx.createVideoContext(videoId,this);
		// 				videoContext1.stop();
		// 			}
					
		// 		}else{
		// 			this.setData({
		// 				['tableData['+i+'].play'] : false
		// 			})
		// 			let videoId1 = 'myVideo'+this.data.tableData[i].id;
		// 			let videoContext2 = wx.createVideoContext(videoId1,this);
		// 			videoContext2.stop();
		// 		}
		// 	}
		// },
		// 全屏/退出全屏
		fullScreen() {
			for(var i = 0; i<this.data.tableData.length; i++){
				if (e.currentTarget.dataset['index'] === this.data.tableData[i].id){
					let videoId = 'myVideo'+this.data.tableData[i].id;
					let videoContext1 = wx.createVideoContext(videoId,this);					
					!this.data.tableData[i].full ? videoContext1.requestFullScreen() : videoContext1.exitFullScreen();
					this.setData({
						['tableData['+i+'].full'] : !this.data.full
					})
				}
			}
		},
		// 完成一次拖动后触发的事件
		sliderChange(e) {
			for(var i = 0; i<this.data.tableData.length; i++){
				if (e.currentTarget.dataset['index'] === this.data.tableData[i].id){
					let videoId = 'myVideo'+this.data.tableData[i].id;
					let videoContext1 = wx.createVideoContext(videoId,this);					
					if (this.data.tableData[i].duration) {
						// 视频跳转到指定位置
						videoContext1.seek(e.detail.value / 100 * this.data.tableData[i].duration);
						this.setData({
							['tableData['+i+'].sliderValue']: e.detail.value,
							['tableData['+i+'].updateState'] : true
						})
					}
				}
			}
		},
		// 拖动过程中触发的事件
		sliderChanging(e) {
			for(var i = 0; i<this.data.tableData.length; i++){
				if (e.currentTarget.dataset['index'] === this.data.tableData[i].id){
						this.setData({
							['tableData['+i+'].updateState']: false// 拖动过程中不允许更新进度条
						})
				}
			}
		},
		// 播放进度
		bindtimeupdateFun(e) {
			for(var i = 0; i<this.data.tableData.length; i++){
				if (e.currentTarget.dataset['index'] === this.data.tableData[i].id){
					// if(this.data.tableData[i].updateState){
						let sliderValue = e.detail.currentTime / e.detail.duration * 100;
						this.setData({
							['tableData['+i+'].sliderValue']: sliderValue,
							['tableData['+i+'].duration']: e.detail.duration,
							['tableData['+i+'].total_duration']: this.formatSeconds(e.detail.duration), // 总时长 格式 00:00
							['tableData['+i+'].process_duration']: this.formatSeconds(e.detail.currentTime), // 进度 格式 00:00
						})
					// }
				}
			}
		},
		// 结束
		bindEndedFun(e){
			for(var i = 0; i<this.data.tableData.length; i++){
				if (e.currentTarget.dataset['index'] === this.data.tableData[i].id){
						this.setData({
							['tableData['+i+'].play']: false// 拖动过程中不允许更新进度条
						})
						let videoId = 'myVideo'+this.data.tableData[i].id;
						let videoContext1 = wx.createVideoContext(videoId,this);
						videoContext1.stop();
				}
			}
		},
		// 格式化视频时长  格式 00:00
		formatSeconds(value) {
			if (value == undefined) {
				value = 0;
			}
			var second = parseInt(value); // 秒
			var min = 0; // 分
			var hour = 0; // 小时
			if (second > 60) {
				min = parseInt(second / 60);
				second = parseInt(second % 60);
				if (min > 60) {
					hour = parseInt(min / 60);
					min = parseInt(min % 60);
				}
			}
			
			var result = "";
			// if (hour > 0) {
			//   hour = addZero(parseInt(hour));
			//   result = hour + ":";
			// }
			if (min > 0) {
				min = addZero(parseInt(min));
				result = min + ":";
			}else{
				result = "00:";
			}
			if(second > 0){
				second = addZero(parseInt(second));
				result = result + second;
			}else{
				result = result + '00';
			}
			return result;
		},
	},
	
})